package cn.com.dhcc.app.pub.core.util.fusioncharts;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * fusioncharts生成xml工具类
 * 
 * @日期：2014-6-18 上午9:17:16
 * @作者：张洪涛
 * @版权所有：东华软件
 * @版本：1.0
 */
public class ChartXmlUtil {

	public static String forXML(Chart chart){
		String strXML="";
		strXML = "<chart "+chart.getExtProperty()+" >";
		if(chart.getCategories() != null && chart.getCategories() != null){
			strXML = strXML + "<categories >";
			for(int i = 0;i < chart.getCategories().getCategory().size() ; i++){
				strXML = strXML +"<category label='"+chart.getCategories().getCategory().get(i).getLabel()+"' />";
			}
			strXML = strXML + "</categories >";
		}
		if(chart.getDataset() !=null && chart.getDataset().size() > 0 ){
			for (int i = 0; i < chart.getDataset().size(); i++) {
				strXML = strXML + "<dataset seriesName='"+chart.getDataset().get(i).getSeriesName()+"' color='"+chart.getDataset().get(i).getColor()+"'  anchorBorderColor='"+chart.getDataset().get(i).getAnchorBorderColor()+"'>";
				for(int j = 0; j < chart.getDataset().get(i).getSet().size(); j++){
					String value = chart.getDataset().get(i).getSet().get(j).getValue();
					strXML = strXML + "<set toolText='" + value + "' value='" + value +"'/>";
				}
				strXML = strXML + "</dataset >";
			}
		}
		if(chart.getSet() != null && chart.getSet().size() > 0 ){
			for(int i = 0; i< chart.getSet().size(); i++){
				ChartSet set = chart.getSet().get(i);
				String highLight = "";
				if(set.getAnchorBgColor() != null && set.getAnchorRadius() != null) {
					highLight = "' anchorBgColor='" + set.getAnchorBgColor() + "' anchorRadius='" + set.getAnchorRadius();
				}
				strXML = strXML + "<set label='"+chart.getSet().get(i).getLabel()+"' value='"+chart.getSet().get(i).getValue()+ highLight + "'/>";
			}
		}
		strXML = strXML + "</chart>";
		return strXML;
	}
	
	public static String getXmlForColumn(String caption, Categories cate, List<ChartDataset> dataSetList, String suffix) {
		//create a chart object
		Chart myChart = new Chart();
		
		myChart.setPalette("2");
		myChart.setCaption(caption);
		myChart.setShowSum("0");
		String numberFormat = " numberScaleValue='1000' numberScaleUnit='（千）'";
		String ep = "palette=" + "\"" + myChart.getPalette() + "\"" + " caption=" + "\"" + myChart.getCaption() + "\"" + " showvalues=" + "\"" + myChart.getShowValues() + "\"" 
					 + " useRoundEdges=" + "\"" + myChart.getUseRoundEdges() + "\"" + "seriesNameInToolTip = '0'" + numberFormat + " numberSuffix='" + suffix + "'";
		myChart.setExtProperty(ep);
		
		//set Categories
		myChart.setCategories(cate);
		
		//set datasets i.e. receive and sent
		myChart.setDataset(dataSetList);
		
		return forXML(myChart);
	}
	
	public static String getXmlForLine(String caption, Categories cate, List<ChartDataset> dataSetList) {
		//create a chart object
		Chart myChart = new Chart();
		
		myChart.setPalette("2");
		myChart.setCaption(caption);
		myChart.setShowSum("0");
		
		String captionString = "";
		if(caption.equals("")) {
			captionString = " ";
		} else {
			captionString = " caption='" + myChart.getCaption() + "'";
		}
		
		String numberFormat = " numberScaleValue='1000' numberScaleUnit='（千）'";
		String ep = "palette=" + "'" + myChart.getPalette() + "'" + captionString + " showvalues=" + "'" + myChart.getShowValues() + "'" 
		             + " alternateHGridAlpha='20'"
		             + " canvasPadding='30'"
		             + " divLineColor='FCB541' divLineAlpha='50'"
		             + " canvasBorderColor='666666' baseFontColor='666666' lineColor='FCB541'"
					 + " useRoundEdges=" + "'" + myChart.getUseRoundEdges() + "'"
					 + numberFormat;
		
		//labelDisplay='Rotate' slantLabels='1' 
		myChart.setExtProperty(ep);
		
		//set Categories
		myChart.setCategories(cate);
		
		//set datasets i.e. receive and sent
		myChart.setDataset(dataSetList);
		
		return forXML(myChart);
	}
	
	public static String getXmlForOnlyLine(List<ChartSet> sets) {
		//create a chart object
		Chart myChart = new Chart();
		
		String ep = " bgColor='#FFFFFF' lineColor='FCB541' showYAxisvalue='0' "
				  + " divLineIsDashed='1' showValues='0' "
				  + " numDivLines='0' showBorder='0' "
				  + " canvasBorderAlpha='0'"; //chartLeftMargin='0'";
		
		myChart.setExtProperty(ep);

		List<String> values = new ArrayList<String>();
		for(ChartSet set: sets) {
			if(set.getValue() != null)
				values.add(set.getValue());
		}
		
		//排序
		Collections.sort(values, new Comparator<Object>() {

			@Override
			public int compare(Object o1, Object o2) {

				return new Integer((String) o1)
						.compareTo(new Integer((String) o2));

			}

		});
		
		String max = values.get(values.size() - 1);
		
		for(int i = 0; i < sets.size(); i++) {
			if(sets.get(i).getValue() != null) {	
				if(sets.get(i).getValue().equals("0")) {
					ChartSet maxSet = sets.get(i);
					maxSet.setAnchorBgColor("808080");
					maxSet.setAnchorRadius("4.6");
					sets.set(i, maxSet);
				} else if(sets.get(i).getValue().equals(max)) {
					ChartSet maxSet = sets.get(i);
					maxSet.setAnchorBgColor("FF0000");
					maxSet.setAnchorRadius("4.6");
					sets.set(i, maxSet);
				}
			}
		}
				
		myChart.setSet(sets);
		return forXML(myChart);
	}
	
	public static void setHeader(Chart chart, String caption) {
		chart.setPalette("2");
		chart.setCaption(caption);
		chart.setShowSum("0");
		String ep = "palette=" + "\"" + chart.getPalette() + "\"" + " caption=" + "\"" + chart.getCaption() + "\"" + " showvalues=" + "\"" + chart.getShowValues() + "\"" 
					 + " useRoundEdges=" + "\"" + chart.getUseRoundEdges() + "\"";
		chart.setExtProperty(ep);
	}
	
	public static void main(String[] args) {
		
		Chart myChart = new Chart();
		
		//set chart properties
		myChart.setPalette("2");
		myChart.setCaption("24小时统计");
		myChart.setShowSum("0");
		String ep = "palette=" + "\"" + myChart.getPalette() + "\"" + " caption=" + "\"" + myChart.getCaption() + "\"" + " showvalues=" + "\"" + myChart.getShowValues() + "\"" 
				    + " useRoundEdges=" + "\"" + myChart.getUseRoundEdges() + "\"";
		myChart.setExtProperty(ep);
		
		List<ChartSet> sets = new ArrayList<ChartSet>();
		for(int i = 0; i <= 24; i++) {
			sets.add(new ChartSet(i + " - " + i+1, "3"));
		}
		myChart.setSet(sets);
		
		
		//generate xml file
		//ChartXmlUtil.forXML(myChart);
		System.out.println(ChartXmlUtil.forXML(myChart));
	}
//	public static void main(String[] args) {
//		Chart chart = new Chart();
//		chart.setPalette("2");
//		chart.setCaption("Monthly Sales Summary");
//		chart.setSubCaption("For the year 2006");
//		chart.setxAxisName("Month");
//		chart.setyAxisName("Sales");
//		chart.setNumberPrefix("$");
//		chart.setShowValues("0");
//		
//		Categories categories = new Categories();
//		Category c1 = new Category();
//		List<Category> cList = new ArrayList<Category>();
//		c1.setLabel("Austria");
//		Category c2 = new Category();
//		Category c3 = new Category();
//		Category c4 = new Category();
//		Category c5 = new Category();
//		c2.setLabel("Brazil");
//		c3.setLabel("France");
//		c4.setLabel("Germany");
//		c5.setLabel("USA");
//		cList.add(c1);
//		cList.add(c2);
//		cList.add(c3);
//		cList.add(c4);
//		cList.add(c5);
//		categories.setCategory(cList);
//		chart.setCategories(categories);
//		
//		List<ChartDataset> dList = new ArrayList<ChartDataset>();
//		ChartDataset d1 = new ChartDataset();
//		d1.setSeriesName("1996");
//		d1.setShowValues("0");
//		d1.setColor("AFD8F8");
//		
//		List<ChartSet> sets1 = new ArrayList<ChartSet>();
//		ChartSet set11 = new ChartSet();
//		set11.setValue("25601.34");
//		ChartSet set12 = new ChartSet();
//		set12.setValue("20148.82");
//		ChartSet set13 = new ChartSet();
//		set13.setValue("17372.76");
//		ChartSet set14 = new ChartSet();
//		set14.setValue("35407.15");
//		ChartSet set15 = new ChartSet();
//		set15.setValue("38105.68");
//		sets1.add(set11);
//		sets1.add(set12);
//		sets1.add(set13);
//		sets1.add(set14);
//		sets1.add(set15);
//		d1.setSet(sets1);
//		
//		ChartDataset d2 = new ChartDataset();
//		
//		d2.setSeriesName("1997");
//		d2.setShowValues("0");
//		d2.setColor("F6BD0F");
//		
//		List<ChartSet> sets2 = new ArrayList<ChartSet>();
//		ChartSet set21 = new ChartSet();
//		set21.setValue("57401.85");
//		ChartSet set22 = new ChartSet();
//		set22.setValue("41941.19");
//		ChartSet set23 = new ChartSet();
//		set23.setValue("45263.37");
//		ChartSet set24 = new ChartSet();
//		set24.setValue("117320.16");
//		ChartSet set25 = new ChartSet();
//		set25.setValue("114845.27");
//		sets2.add(set21);
//		sets2.add(set22);
//		sets2.add(set23);
//		sets2.add(set24);
//		sets2.add(set25);
//		d2.setSet(sets2);
//		dList.add(d1);
//		dList.add(d2);
//		chart.setDataset(dList);
//		
//		List<Set> sets = new ArrayList<Set>();
//		Set set1 = new Set();
//		set1.setLabel("Jan");
//		set1.setValue("17400");
//		Set set2 = new Set();
//		set2.setLabel("Feb");
//		set2.setValue("18100");
//		Set set3 = new Set();
//		set3.setLabel("Mar");
//		set3.setValue("21800");
//		Set set4 = new Set();
//		set4.setLabel("Apr");
//		set4.setValue("23800");
//		Set set5 = new Set();
//		set5.setLabel("May");
//		set5.setValue("29600");
//		sets.add(set1);
//		sets.add(set2);
//		sets.add(set3);
//		sets.add(set4);
//		sets.add(set5);
//		
//		chart.setSet(sets);
		
//		System.out.println(forXML(chart));
//	}

}
